function clickAction(videoSourceId) {
	var thisvsid = videoSourceId;
}

$(function() {

	function getLocalTime(nS) {
		return new Date(parseInt(nS)).toLocaleString().substr(0, 10).replace("/", "-").replace("/", "-");
	}

	function formatSeconds(value) {
		var theTime = parseInt(value); // 秒
		var theTime1 = 0; // 分
		var theTime2 = 0; // 小时
		if (theTime > 60) {
			theTime1 = parseInt(theTime / 60);
			theTime = parseInt(theTime % 60);
			if (theTime1 > 60) {
				theTime2 = parseInt(theTime1 / 60);
				theTime1 = parseInt(theTime1 % 60);
			}
		}
		var result = "" + parseInt(theTime) + "秒";
		if (theTime1 > 0) {
			result = "" + parseInt(theTime1) + "分" + result;
		}
		if (theTime2 > 0) {
			result = "" + parseInt(theTime2) + "小时" + result;
		}
		return result;
	}

	var removeChildNodes = function($, own) {
		if ($ == null) {
			return;
		}
		while ($.hasChildNodes()) {
			$.removeChild($.firstChild);
		}
		if (own) {
			$.parentNode.removeChild($);
		}
	};

	$(function(e) {
		var $area = $('.search-area'),
			$showhide = $('.showhide-search'),
			$input = $('.search-input');
		$('.course-nav-hd').on('click', '.showhide-search', function(event) {
			var isShow = $(this).attr('data-show');
			if (isShow == 'no') {
				show()
				$input.focus();
				$input.keyup(function() {
					$(".search-result").css("display", "block");
				});
				$(".search-input").css("border", "1px solid #bcbcbc");
			} else {
				if ($input.val() == "") {
					hide()
				}
			}
			return false;
		})
		$('.course-nav-hd').on('click', '.search-area', function(event) {
			return false;
		})
		$(document).on('click', function() {
			if ($input.val() != '') {
				hide()
			} else {
				hide()
			}
		})
		var show = function() {
			$showhide.attr('data-show', 'yes')
			$area.show(1, function() {
				$area.removeClass('min')
			})
		}
		var hide = function() {
			$showhide.attr('data-show', 'no')
			$area.fadeOut('slow', function() {
				$area.addClass('min')
			})
		}
	})

	$('.quanb').on('click', function() {
		window.location.reload();
	})

	//全局变量
	var pageContext = {
		mainTypeId: 0,
		childType: 0,
		pageNum: 1,
		page: 1,
		order: 1
	}

	//课程方向
	$('.main-f').css({
		"color": "#fff",
		"background": "#51c0eb"
	});
	$.ajax({
		type: "GET",
		url: $url + '/type/getMainType/' + pageContext.mainTypeId,
		dataType: "json",
		success: function(result) {
			var items = result.data.list;
			$.each(items, function(i, value) {
				$('<li/>', {
					html: '<a href="javascript:;">' + value.mainTypeName + '</a>'
				}).appendTo('.' + 'ul').addClass("course-nav-item").on('click', function() {
					$(".main-f").css({
						"color": "#757575",
						"background": "#fff"
					});
					$(".main-l").css({
						"color": "#fff",
						"background": "#5ac0eb"
					});
					$(this).attr("id", value.mainTypeId);
					$(this).addClass("on").siblings().removeClass("on");
					$(this).siblings().removeAttr("id");
					var thisid = $(this).attr('id');
					$('#zuir').on('click', function() {
						$(this).css({
							"color": "#5ac0eb"
						})
						$('#zuix').css({
							"color": "#757575"
						})
						pageContext.order = 2;

					});
					$('#zuix').on('click', function() {
						$(this).css({
							"color": "#5ac0eb"
						})
						$('#zuir').css({
							"color": "#757575"
						})
						pageContext.order = 1;

					});
					
					var thistyname = value.mainTypeName;
					alert(thistyname);
					onecliCk(thisid,thistyname);

				});
			});
		}
	});

	//课程分类

	$(".main-l").css({
		"color": "#fff",
		"background": "#5ac0eb"
	});
	$.ajax({
		type: "GET",
		url: $url + '/type/getAllChildType/' + pageContext.childType,
		dataType: "json",
		success: function(result) {
			var items = result.data.list;
			$.each(items, function(i, value) {
				$('<li/>', {
					html: '<a href="javascript:;">' + value.childTypeName + '</a>'
				}).appendTo('.' + 'ul-course').addClass("course-nav-item").on('click', function() {
					$(this).attr("id", value.childTypeId);
					$(".main-l").css({
						"color": "#757575",
						"background": "#fff"
					});
					$(this).addClass("on").siblings().removeClass("on");
					$(this).siblings().removeAttr("id");
					var thischildid = $(this).attr('id');
					console.log(thischildid)
					twocliCk(thischildid);

				});
			});
		},
		error: function(jqXHR) {
			alert("发生错误：" + jqXHR.status)
		}
	}); //课程分类默认列表结束

	//默认视频列表
	$.ajax({
		type: "GET",
		url: $url + '/video/getAllVideoList?page=' + pageContext.pageNum + '&orderMethod=' + pageContext.order + '',
		dataType: "json",
		success: function(result) {
			var pagesize = result.data.pageCount;
			var items = result.data.list;
			if (pagesize > 0) {
				$.jqPaginator('#pagination1', {
					totalPages: pagesize, //设置分页的总页数
					visiblePages: 10, //最多显示的页数
					currentPage: 1, //设置当前的页数
					first: '<li class="first"><a href="javascript:void(0);">首页<\/a><\/li>',
					prev: '<li class="prev"><a href="javascript:void(0);"><i class="arrow arrow2"><\/i>上一页<\/a><\/li>',
					next: '<li class="next"><a href="javascript:void(0);">下一页<i class="arrow arrow3"><\/i><\/a><\/li>',
					last: '<li class="last"><a href="javascript:void(0);">末页<\/a><\/li>',
					page: '<li class="page"><a href="javascript:void(0);">{{page}}<\/a><\/li>',
					onPageChange: function(n) {
						$(".pager-cur").html(n);
						$(".pager-total").html(pagesize);
						$('#ul-course4 li').remove();
						$.ajax({
							type: "GET",
							url: $url + '/video/getAllVideoList?page=' + n + '&orderMethod=' + pageContext.order + '',
							dataType: 'json',
							success: function(result) {
								var items = result.data.list;
								$.each(items, function(i, value) {
									$('<li/>', {
										html: '<a href="http://192.168.0.12:8080/elink_video/video/video.html?videoSourceId=' + value.videoSourceId + '">' +
											'<div class="figure">' +
											'<img src="http://192.168.0.12:8080/elink_video/resources' + value.snapshotUrl + '"/>' +
											'</div>' +
											'<h5 class="title">' + value.video_source_name +
											'<span class="fr">' + formatSeconds(value.videoLength) +
											'</span>' +
											'</h5>' +
											'<p class="desc">' + value.describes +
											'</p>' +
											'<div class="author-warp">' +
											'<span class="author fl">' + value.videoSourceId +
											'</span>' +
									
									'<span class="time">' + getLocalTime(value.createTime) +
											'</span>' +
											'<span class="num fr">播放量：' +
											'<em>' + value.playTimes +
											'</em>' +
											'</span>' +
											'</div>' +
											'</a>'
									}).appendTo('.' + 'ul-course4').addClass("course-nav-item");
								})
							}
						})
					}
				});
			} else {
				$('#pagination1').jqPaginator('destroy');
			}

		},
		error: function(jqXHR) {
			alert("发生错误：" + jqXHR.status)
		}
	});

	/*
	 * 点击菜单事件封装
	 *
	 * function onecliCk(){
	 * 	 给一级选项绑定点击事件
	 * 	点击之前先清楚一次视频列表 和 2级菜单
	 * 	removeChildNode('#'+'ul-course4');
	 * 	removeChildNode('#'+'bd');
	 * 	点击一级的时候显示this下面的二级菜单   和当前一级菜单下面的所有视频列表
	 * 	$.ajax({
	 * 		})
	 * }
	 * function twocliCk(){
	 * 	给二级选项绑定点击事件
	 * 	点击之前先清楚一次视频列表
	 * 	removeChildNode('#'+'ul-course4');
	 * 	点击显示this 对应的视频列表 的第1页 
	 * 	$.ajax({
	 * 		
	 *      })
	 * }
	 * function allOne(){
	 * 		一级菜单全部按钮点击事件
	 * 		清除二级菜单列表
	 * 		removeChildNode('#'+'bd');
	 * 		清除视频列表
	 * 		removeChildNode("#"+'ul-course4');
	 * 		点击显示全部二级菜单 全部视频列表第1页
	 * 		
	 * }
	 * function allTwo(){
	 * 		清楚二级菜单列表
	 * 		removeChildNode('#'+'bd');
	 * 		清楚视频列表
	 * 		removeChildNode("#"+'ul-course4');
	 * 		显示全部二级菜单 全部视频列表第一页
	 * }
	 */

	function onecliCk(thisid,thistyname) {
		$('#bd li').remove();
		$('#ul-course4 li').remove();
		$('#pagination1').jqPaginator('destroy');
		$.jqPaginator('#pagination1', {
			totalPages: 1, //设置分页的总页数
			visiblePages: 10, //最多显示的页数
			currentPage: 1, //设置当前的页数
			first: '<li class="first"><a href="javascript:void(0);">首页<\/a><\/li>',
			prev: '<li class="prev"><a href="javascript:void(0);"><i class="arrow arrow2"><\/i>上一页<\/a><\/li>',
			next: '<li class="next"><a href="javascript:void(0);">下一页<i class="arrow arrow3"><\/i><\/a><\/li>',
			last: '<li class="last"><a href="javascript:void(0);">末页<\/a><\/li>',
			page: '<li class="page"><a href="javascript:void(0);">{{page}}<\/a><\/li>',
			onPageChange: function(n) {
				$(".pager-cur").html(n)
				$(".pager-total").html(1);
			}
		});
		$.ajax({
			type: "GET",
			url: $url + '/type/getChildType/' + thisid,
			dataType: "json",
			success: function(result) {
				var items = result.data.list;
				$.each(items, function(i, value) {
					$('<li/>', {
						html: '<a href="javascript:;">' + value.childTypeName + '</a>'
					}).appendTo('#bd').addClass("course-nav-item").on('click', function() {
						var thischildname = value.childTypeName;
						$(".main-f").css({
							"color": "#757575",
							"background": "#fff"
						});
						$(".main-l").css({
							"color": "#757575",
							"background": "#fff"
						});
						$(this).attr('id', value.childTypeId);
						$(this).addClass("on").siblings().removeClass("on");
						$(this).siblings().removeAttr("id");
						//清空视频列表  清空分页
						$('#ul-course4 li').remove();
						$('#pagination1').jqPaginator('destroy');
						$.jqPaginator('#pagination1', {
							totalPages: 1, //设置分页的总页数
							visiblePages: 10, //最多显示的页数
							currentPage: 1, //设置当前的页数
							first: '<li class="first"><a href="javascript:void(0);">首页<\/a><\/li>',
							prev: '<li class="prev"><a href="javascript:void(0);"><i class="arrow arrow2"><\/i>上一页<\/a><\/li>',
							next: '<li class="next"><a href="javascript:void(0);">下一页<i class="arrow arrow3"><\/i><\/a><\/li>',
							last: '<li class="last"><a href="javascript:void(0);">末页<\/a><\/li>',
							page: '<li class="page"><a href="javascript:void(0);">{{page}}<\/a><\/li>',
							onPageChange: function(n) {
								$(".pager-cur").html(n)
							}
						});
						//动态生成视频列表  分页
						var thistwoid = $(this).attr("id");
						$.ajax({
							type: "GET",
							url: $url + '/video/getChildTypeVideoList?childTypeId=' + thistwoid + '&page=' + 1 + '&orderMethod=' + pageContext.order + '',
							dataType: 'json',
							success: function(result) {
								if (result.data != null) {
									var pagesize = result.data.pageCount;
									var items = result.data.list;
									if (pagesize > 0) {
										$.jqPaginator('#pagination1', {
											totalPages: pagesize, //设置分页的总页数
											visiblePages: 10, //最多显示的页数
											currentPage: 1, //设置当前的页数
											first: '<li class="first"><a href="javascript:void(0);">首页<\/a><\/li>',
											prev: '<li class="prev"><a href="javascript:void(0);"><i class="arrow arrow2"><\/i>上一页<\/a><\/li>',
											next: '<li class="next"><a href="javascript:void(0);">下一页<i class="arrow arrow3"><\/i><\/a><\/li>',
											last: '<li class="last"><a href="javascript:void(0);">末页<\/a><\/li>',
											page: '<li class="page"><a href="javascript:void(0);">{{page}}<\/a><\/li>',
											onPageChange: function(n) {
												$(".pager-cur").html(n)
												$(".pager-total").html(pagesize);
												$('#ul-course4 li').remove();
												$.ajax({
													type: "GET",
													url: $url + '/video/getChildTypeVideoList?childTypeId=' + thistwoid + '&page=' + n + '&orderMethod=' + pageContext.order + '',
													dataType: 'json',
													success: function(result) {
														var items = result.data.list;
														$.each(items, function(i, value) {
															$('<li/>', {
																html: '<a href="http://192.168.0.12:8080/elink_video/video/video.html?videoSourceId=' + value.videoSourceId + '&childTypeName='+thischildname+'&mainTypeName='+thistyname+'">' +
																	'<div class="figure">' +
																	'<img src="http://192.168.0.12:8080/elink_video/resources' + value.snapshotUrl + '"/>' +
																	'</div>' +
																	'<h5 class="title">' + value.video_source_name +
																	'<span class="fr">' + formatSeconds(value.videoLength) +
																	'</span>' +
																	'</h5>' +
																	'<p class="desc">' + value.describes +
																	'</p>' +
																	'<div class="author-warp">' +
																	'<span class="author fl">' + value.videoSourceId +
																	'</span>' +
																	'<span class="time">' + getLocalTime(value.createTime) +
																	'</span>' +
																	'<span class="num fr">播放量：' +
																	'<em>' + value.playTimes +
																	'</em>' +
																	'</span>' +
																	'</div>' +
																	'</a>'
															}).appendTo('.' + 'ul-course4').addClass("course-nav-item");
														})
													}
												})
											}
										})
									}
								} else {
									return false;
								}

							}
						})
					});
				});
			}
		})
		$.ajax({
			type: "GET",
			url: $url + '/video/getMainTypeVideoList?mainTypeId=' + thisid + '&page=' + 1 + '&orderMethod=' + pageContext.order + '',
			dataType: "json",
			success: function(result) {
				var pagesize = result.data.pageCount;
				var items = result.data.list;
				if (pagesize > 0) {
					$.jqPaginator('#pagination1', {
						totalPages: pagesize, //设置分页的总页数
						visiblePages: 10, //最多显示的页数
						currentPage: 1, //设置当前的页数
						first: '<li class="first"><a href="javascript:void(0);">首页<\/a><\/li>',
						prev: '<li class="prev"><a href="javascript:void(0);"><i class="arrow arrow2"><\/i>上一页<\/a><\/li>',
						next: '<li class="next"><a href="javascript:void(0);">下一页<i class="arrow arrow3"><\/i><\/a><\/li>',
						last: '<li class="last"><a href="javascript:void(0);">末页<\/a><\/li>',
						page: '<li class="page"><a href="javascript:void(0);">{{page}}<\/a><\/li>',
						onPageChange: function(n) {
							$(".pager-cur").html(n)
							$(".pager-total").html(pagesize);
							$('#ul-course4 li').remove();
							$.ajax({
								type: "GET",
								url: $url + '/video/getMainTypeVideoList?mainTypeId=' + thisid + '&page=' + n + '&orderMethod=' + pageContext.order + '',
								dataType: 'json',
								success: function(result) {
									var items = result.data.list;
									$.each(items, function(i, value) {
										$('<li/>', {
											html: '<a href="http://192.168.0.12:8080/elink_video/video/video.html?videoSourceId=' + value.videoSourceId + '">' +
												'<div class="figure">' +
												'<img src="http://192.168.0.12:8080/elink_video/resources' + value.snapshotUrl + '"/>' +
												'</div>' +
												'<h5 class="title">' + value.video_source_name +
												'<span class="fr">' + formatSeconds(value.videoLength) +
												'</span>' +
												'</h5>' +
												'<p class="desc">' + value.describes +
												'</p>' +
												'<div class="author-warp">' +
												'<span class="author fl">' + value.videoSourceId +
												'</span>' +
												'<span class="time">' + getLocalTime(value.createTime) +
												'</span>' +
												'<span class="num fr">播放量：' +
												'<em>' + value.playTimes +
												'</em>' +
												'</span>' +
												'</div>' +
												'</a>'
										}).appendTo('.' + 'ul-course4').addClass("course-nav-item");
									})
								}
							})
						}
					});
				} else {
					$('#pagination1').jqPaginator('destroy');
				}

			},
			error: function(jqXHR) {
				alert("发生错误：" + jqXHR.status)
			}
		});

	} //主菜单点击fn结束

	function twocliCk(thischildid) {
		$('#ul-course4 li').remove();
		$('#pagination1').jqPaginator('destroy');
		$.jqPaginator('#pagination1', {
			totalPages: 1, //设置分页的总页数
			visiblePages: 10, //最多显示的页数
			currentPage: 1, //设置当前的页数
			first: '<li class="first"><a href="javascript:void(0);">首页<\/a><\/li>',
			prev: '<li class="prev"><a href="javascript:void(0);"><i class="arrow arrow2"><\/i>上一页<\/a><\/li>',
			next: '<li class="next"><a href="javascript:void(0);">下一页<i class="arrow arrow3"><\/i><\/a><\/li>',
			last: '<li class="last"><a href="javascript:void(0);">末页<\/a><\/li>',
			page: '<li class="page"><a href="javascript:void(0);">{{page}}<\/a><\/li>',
			onPageChange: function(n) {
				$(".pager-cur").html(n)
				$(".pager-total").html(1);
			}
		});

		$.ajax({
				type: "GET",
				url: $url + '/video/getChildTypeVideoList?childTypeId=' + thischildid + '&page=1&orderMethod=' + pageContext.order + '',
				dataType: "json",
				success: function(result) {
					if (result.data != null) {
						var pagesize = result.data.pageCount;
						var items = result.data.list;
						if (pagesize > 0) {
							$.jqPaginator('#pagination1', {
								totalPages: pagesize, //设置分页的总页数
								visiblePages: 10, //最多显示的页数
								currentPage: 1, //设置当前的页数
								first: '<li class="first"><a href="javascript:void(0);">首页<\/a><\/li>',
								prev: '<li class="prev"><a href="javascript:void(0);"><i class="arrow arrow2"><\/i>上一页<\/a><\/li>',
								next: '<li class="next"><a href="javascript:void(0);">下一页<i class="arrow arrow3"><\/i><\/a><\/li>',
								last: '<li class="last"><a href="javascript:void(0);">末页<\/a><\/li>',
								page: '<li class="page"><a href="javascript:void(0);">{{page}}<\/a><\/li>',
								onPageChange: function(n) {
									$(".pager-cur").html(n)
									$(".pager-total").html(pagesize);
									$('#ul-course4 li').remove();
									$.ajax({
										type: "GET",
										url: $url + '/video/getChildTypeVideoList?childTypeId=' + thischildid + '&page=' + n + '&orderMethod=' + pageContext.order + '',
										dataType: 'json',
										success: function(result) {
											var items = result.data.list;
											$.each(items, function(i, value) {
												$('<li/>', {
													html: '<a href = "http://192.168.0.12:8080/elink_video/video/video.html?videoSourceId=' + value.videoSourceId + '">' +
														'<div class="figure">' +
														'<img src="http://192.168.0.12:8080/elink_video/resources' + value.snapshotUrl + '"/>' +
														'</div>' +
														'<h5 class="title">' + value.video_source_name +
														'<span class="fr">' + formatSeconds(value.videoLength) +
														'</span>' +
														'</h5>' +
														'<p class="desc">' + value.describes +
														'</p>' +
														'<div class="author-warp">' +
														'<span class="author fl">' + value.videoSourceId +
														'</span>' +
														'<span class="time">' + getLocalTime(value.createTime) +
														'</span>' +
														'<span class="num fr">播放量：' +
														'<em>' + value.playTimes +
														'</em>' +
														'</span>' +
														'</div>' +
														'</a>'
												}).appendTo('.' + 'ul-course4').addClass("course-nav-item");
											})
										}
									})
								}
							});
						} else {
							$('#pagination1').jqPaginator('destroy');
						}
					} else {
						return false;
					}
				}
			}) //视频列表请求

	}

	function allOne() {
		$('#bd li').remove();
		$.ajax({
			type: "GET",
			url: $url + '/api/getChildType/' + pageContext.mainTypeId,
			dataType: "json",
			success: function(result) {
				var items = result.data.list;
				$.each(items, function(i, value) {
					$('<li/>', {
						html: '<a href="javascript:;">' + value.mainTypeName + '</a>'
					}).appendTo('.' + 'ul').addClass("course-nav-item").on('click', function() {
						$(".main-f").css({
							"color": "#757575",
							"background": "#fff"
						});
						$(".main-l").css({
							"color": "#fff",
							"background": "#5ac0eb"
						});
						$(this).attr("id", value.mainTypeId);
						$(this).addClass("on").siblings().removeClass("on");
						$(this).siblings().removeAttr("id");
					});
				});
			}
		})
	}

});